﻿Imports System
Imports System.Web
Imports System.Web.UI
Imports System.Data
Imports System.Xml
Imports System.Xml.XPath
Imports System.Xml.Xsl
Imports System.IO
Imports System.Net
Imports System.Text
Imports BusinessLogic
Imports Yahoo.API

Partial Class UserCuntrols_TopMenu
    Inherits System.Web.UI.UserControl
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Page.ClientScript.RegisterClientScriptBlock(Me.GetType, "prototype", [String].Format("<script type=""text/javascript"" src=""{0}""></script>", ResolveClientUrl("~/Javascript/prototype.js")))
        'Page.ClientScript.RegisterClientScriptBlock(Me.GetType, "mootools", [String].Format("<script type=""text/javascript"" src=""{0}""></script>", ResolveClientUrl("~/Javascript/mootools.js")))
        'Page.ClientScript.RegisterClientScriptBlock(Me.GetType, "pedro", [String].Format("<script type=""text/javascript"" src=""{0}""></script>", ResolveClientUrl("~/Javascript/pedro.js")))
        'Page.ClientScript.RegisterClientScriptBlock(Me.GetType, "element", [String].Format("<script type=""text/javascript"" src=""{0}""></script>", ResolveClientUrl("~/Javascript/element.js")))
        'Page.ClientScript.RegisterClientScriptBlock(Me.GetType, "event", [String].Format("<script type=""text/javascript"" src=""{0}""></script>", ResolveClientUrl("~/Javascript/event.js")))
        'Page.ClientScript.RegisterClientScriptBlock(Me.GetType, "pane", [String].Format("<script type=""text/javascript"" src=""{0}""></script>", ResolveClientUrl("~/Javascript/pane.js")))
        'Page.ClientScript.RegisterClientScriptBlock(Me.GetType, "global-nav", [String].Format("<script type=""text/javascript"" src=""{0}""></script>", ResolveClientUrl("~/Javascript/global-nav.js")))
        'Page.ClientScript.RegisterClientScriptBlock(Me.GetType, "boxover", [String].Format("<script type=""text/javascript"" src=""{0}""></script>", ResolveClientUrl("~/Javascript/boxover.js")))
        If Session("Username") IsNot Nothing Then
            litLogInText.Visible = True
            lblLoginInformation.Visible = True
            lblLoginInformation.Text = Session("Username")
            Dim _Points = New Points()
            _Points.Username = Session("Username").ToString()
            _Points.LoadPointsByUserName()
            litPoints.Text = String.Format("Your total point(s):<span class=""_aspLabel"">{0}</span>", _Points.TotalPoints)
            imgLogin.ImageUrl = "~/images/SignOut.jpg"
            imgSignUp.Visible = False
            If _Points.TotalPoints >= CInt(ConfigurationManager.AppSettings("MinTotalPoints")) Then
                imgProfile.Visible = True
            End If
        Else
            litLogInText.Visible = False
            lblLoginInformation.Visible = False
            imgLogin.ImageUrl = "~/images/SignIn.jpg"
            imgSignUp.Visible = True
        End If

        Select Case _SelectedTab
            Case "Web"
                imgWeb.ImageUrl = "~/images/WebButton2.jpg"
                Exit Select
            Case "Image"
                imgImage.ImageUrl = "~/images/Images2.jpg"
                Exit Select
            Case "News"
                imgNews.ImageUrl = "~/images/News2.jpg"
                Exit Select
            Case "Video"
                imgvideo.ImageUrl = "~/images/Videos2.jpg"
                Exit Select
            Case Else
                Exit Select
        End Select
        GetNewsSearchWebServiceResults()
        Select Case Request("Q")
            Case "World"
                HyperLink1.Attributes.Add("style", "color:#ffffff;")
                Exit Select
            Case "Finance"
                HyperLink2.Attributes.Add("style", "color:#ffffff;")
                Exit Select
            Case "entertainment"
                HyperLink3.Attributes.Add("style", "color:#ffffff;")
                Exit Select
            Case "sports"
                HyperLink4.Attributes.Add("style", "color:#ffffff;")
                Exit Select
        End Select
    End Sub

    Private Sub GetNewsSearchWebServiceResults()
        If Session("SearchResults") IsNot Nothing Then
            newsRes.DataSource = Session("SearchResults")
            newsRes.DataBind()
        Else
            Dim srch As String = ""
            srch &= ConfigurationManager.AppSettings("RecentNews")

            Dim yahoo As New YahooSearchService

            Dim _SearchResults As New ArrayList()
            Dim Count As Integer = 10
            Dim Offset As Integer = 1

            Dim resultSet As Yahoo.API.NewsSearchResponse.ResultSet
            Try
                Dim YahooApiKey As String = ConfigurationManager.AppSettings("YahooAPIKey").ToString
                resultSet = yahoo.NewsSearch(YahooApiKey, srch, "all", Count, Offset, "rank", "en")

                For Each result As Yahoo.API.NewsSearchResponse.ResultType In resultSet.Result
                    Dim _TempSearchResult As New SearchResult()

                    _TempSearchResult.Url = result.Url
                    If result.Title.Length > 60 Then
                        _TempSearchResult.Title = result.Title.Substring(0, 58) & "..."
                    Else
                        _TempSearchResult.Title = result.Title
                    End If
                    _TempSearchResult.Summary = result.Summary
                    _SearchResults.Add(_TempSearchResult)

                Next
            Catch ex As Exception
            End Try
            Session("SearchResults") = _SearchResults
            newsRes.DataSource = Session("SearchResults")
            newsRes.DataBind()
        End If
    End Sub

    Public Class SearchResult
        Private _Url As String
        Property Url() As String
            Get
                Return _Url
            End Get
            Set(ByVal value As String)
                _Url = value
            End Set
        End Property

        Private _Title As String
        Property Title() As String
            Get
                Return _Title
            End Get
            Set(ByVal value As String)
                _Title = value
            End Set
        End Property

        Private _Summary As String
        Property Summary() As String
            Get
                Return _Summary
            End Get
            Set(ByVal value As String)
                _Summary = value
            End Set
        End Property


        Private _PublishDate As String
        Property PublishDate() As String
            Get
                Return _PublishDate
            End Get
            Set(ByVal value As String)
                _PublishDate = value
            End Set
        End Property

        Private _Thumbnail As String
        Property Thumbnail() As String
            Get
                Return _Thumbnail
            End Get
            Set(ByVal value As String)
                _Thumbnail = value
            End Set
        End Property
    End Class

    Dim _SelectedTab As String
    Property SelectedTab() As String
        Get
            Return _SelectedTab
        End Get
        Set(ByVal value As String)
            _SelectedTab = value
        End Set
    End Property
End Class


